package com.epalmpay.controller.apiShopApp;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.push.model.v20160801.*;
import com.epalmpay.commom.BaseController;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.entity.Notice;
import com.epalmpay.service.app.IAppNoticeService;
import com.epalmpay.service.order.IOrderGoodService;
import com.epalmpay.service.order.impl.OrderGoodServiceImpl;
import com.epalmpay.status.Rescode;
import com.epalmpay.util.Const;
import com.epalmpay.util.HttpClient;
import com.epalmpay.util.HttpClientUtil;
import com.epalmpay.util.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/2/26.
 */
@Controller
@RequestMapping(value="/app/notice")
public class NoticeController extends BaseController {

    @Autowired
    private IAppNoticeService noticeService;

    @Autowired
    private IOrderGoodService orderGoodService;

    /**
     * 发送推送信息
     * @param
     * @return
     */
    @RequestMapping(value="/sendNoticeToDevice")
    @ResponseBody
    public BaseOutDTO sendNoticeToDevice(HttpServletRequest request) {
        BaseOutDTO result = new BaseOutDTO();
        String target = request.getParameter("target");
        String title = request.getParameter("title");
        String body = request.getParameter("body");
        int sender = Integer.parseInt(request.getParameter("sender"));
        int groupid = Integer.parseInt(request.getParameter("groupid"));
        int noticetype = Integer.parseInt(request.getParameter("noticetype"));
        int type = Integer.parseInt(request.getParameter("type"));
        int isplay = Integer.parseInt(request.getParameter("isplay"));
        String tvalue = request.getParameter("tvalue");

        String receiver = "";

        PushRequest androidRequest = new PushRequest();
        //内容较大的请求，使用POST请求
        androidRequest.setMethod(MethodType.POST);
        androidRequest.setAppKey(Long.parseLong(Const.APPKEY));
        androidRequest.setTitle(title);
        androidRequest.setBody(body);
        androidRequest.setAcceptFormat(FormatType.JSON);
        androidRequest.setAndroidExtParameters("{\"type\":\""+type+"\",\"isplay\":\""+isplay+"\",\"tvalue\":\""+tvalue+"\"}");
        androidRequest.setAndroidNotificationChannel("1");
        androidRequest.setPushType("NOTICE");
        androidRequest.setAndroidNotifyType("BOTH");
        androidRequest.setAndroidOpenType("ACTIVITY");
        androidRequest.setDeviceType("ANDROID");

        PushRequest iosRequest = new PushRequest();
        //内容较大的请求，使用POST请求
        iosRequest.setMethod(MethodType.POST);
        iosRequest.setAppKey(Long.parseLong(Const.APPKEY));
        iosRequest.setTitle(title);
        iosRequest.setBody(body);
        iosRequest.setAcceptFormat(FormatType.JSON);
        iosRequest.setAppKey(Long.parseLong(Const.APPKEY));
        iosRequest.setPushType("NOTICE"); // 消息类型 MESSAGE NOTICE
        iosRequest.setDeviceType("iOS");
        iosRequest.setIOSMusic("default"); // iOS通知声音
        iosRequest.setIOSNotificationCategory("iOS10 Notification Category");
        iosRequest.setIOSMutableContent(true);
        iosRequest.setIOSApnsEnv(Const.APNSENV);
//        iosRequest.setIOSApnsEnv("PRODUCT");
        iosRequest.setIOSSilentNotification(true);
        iosRequest.setIOSExtParameters("{\"type\":\""+type+"\",\"isplay\":\""+isplay+"\",\"tvalue\":\""+tvalue+"\"}");

        if("ACCOUNT".equals(target)) {//根据账号发送推送
            receiver = request.getParameter("accountlist");
            androidRequest.setTarget("ACCOUNT");
            androidRequest.setTargetValue(receiver);
            iosRequest.setTarget("ACCOUNT");
            iosRequest.setTargetValue(receiver);
        } else if("ALL".equals(target)) {//全部推送
            receiver = "ALL";
            androidRequest.setTarget("ALL");
            androidRequest.setTargetValue("ALL");
            iosRequest.setTarget("ALL");
            iosRequest.setTargetValue("ALL");
        }

        IClientProfile profile = DefaultProfile.getProfile(Const.REGION, Const.ACCESSKEYID, Const.ACCESSKEYSECRET);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        try {
            PushResponse pushNoticeToAndroidResponse = client.getAcsResponse(androidRequest);
            PushResponse pushResponse = client.getAcsResponse(iosRequest);

            //保存到数据库
            Notice notice = new Notice();
            notice.setGroupid(groupid);
            notice.setNoticebody(body);
            notice.setNoticetarget(target);
            notice.setNoticetitle(title);
            notice.setReceiver(receiver);
            notice.setSender(sender);
            notice.setNoticetype(noticetype);
            notice.setIsplay(0);
            noticeService.sendNotice(notice);
            result.setErrorCode(Rescode.SUCCESS, "发送成功");
        } catch (Exception e) {
            result.setErrorCode(Rescode.FAIL, "发送失败");
        }
        return result;
    }

    /**
     * 发送提醒客服信息
     * @param
     * @return
     */
    @RequestMapping(value="/sendNoticeToSeller")
    @ResponseBody
    public BaseOutDTO sendNoticeToSeller(HttpServletRequest request) {
        BaseOutDTO result = new BaseOutDTO();
        String orderId = request.getParameter("orderId");

        Map userinfo = orderGoodService.selectUserInfoById(Long.parseLong(orderId));
        if(userinfo != null) {
            String target = "ACCOUNT";
            String title = "发货提醒";
            String body = "订单" + orderId + "提醒您发货";
            int sender = 0;
            int groupid = Integer.parseInt(userinfo.get("group_id").toString());
            int noticetype = 1;
            int type = 1;
            int isplay = 1;
            String tvalue = request.getParameter("tvalue");

            String receiver = userinfo.get("username").toString();

            PushRequest androidRequest = new PushRequest();
            //内容较大的请求，使用POST请求
            androidRequest.setMethod(MethodType.POST);
            androidRequest.setAppKey(Long.parseLong(Const.APPKEY));
            androidRequest.setTitle(title);
            androidRequest.setBody(body);
            androidRequest.setAcceptFormat(FormatType.JSON);
            androidRequest.setAndroidExtParameters("{\"type\":\"" + type + "\",\"isplay\":\"" + isplay + "\",\"tvalue\":\"" + tvalue + "\"}");
            androidRequest.setAndroidNotificationChannel("1");
            androidRequest.setPushType("NOTICE");
            androidRequest.setAndroidNotifyType("BOTH");
            androidRequest.setAndroidOpenType("ACTIVITY");
            androidRequest.setDeviceType("ANDROID");

            PushRequest iosRequest = new PushRequest();
            //内容较大的请求，使用POST请求
            iosRequest.setMethod(MethodType.POST);
            iosRequest.setAppKey(Long.parseLong(Const.APPKEY));
            iosRequest.setTitle(title);
            iosRequest.setBody(body);
            iosRequest.setAcceptFormat(FormatType.JSON);
            iosRequest.setAppKey(Long.parseLong(Const.APPKEY));
            iosRequest.setPushType("NOTICE"); // 消息类型 MESSAGE NOTICE
            iosRequest.setDeviceType("iOS");
            iosRequest.setIOSMusic("default"); // iOS通知声音
            iosRequest.setIOSNotificationCategory("iOS10 Notification Category");
            iosRequest.setIOSMutableContent(true);
            iosRequest.setIOSApnsEnv(Const.APNSENV);
//            iosRequest.setIOSApnsEnv("PRODUCT");
            iosRequest.setIOSSilentNotification(true);
            iosRequest.setIOSExtParameters("{\"type\":\"" + type + "\",\"isplay\":\"" + isplay + "\",\"tvalue\":\"" + tvalue + "\"}");

            if ("ACCOUNT".equals(target)) {//根据账号发送推送
                receiver = request.getParameter("accountlist");
                androidRequest.setTarget("ACCOUNT");
                androidRequest.setTargetValue(receiver);
                iosRequest.setTarget("ACCOUNT");
                iosRequest.setTargetValue(receiver);
            } else if ("ALL".equals(target)) {//全部推送
                receiver = "ALL";
                androidRequest.setTarget("ALL");
                androidRequest.setTargetValue("ALL");
                iosRequest.setTarget("ALL");
                iosRequest.setTargetValue("ALL");
            }

            IClientProfile profile = DefaultProfile.getProfile(Const.REGION, Const.ACCESSKEYID, Const.ACCESSKEYSECRET);
            DefaultAcsClient client = new DefaultAcsClient(profile);
            try {
                PushResponse pushNoticeToAndroidResponse = client.getAcsResponse(androidRequest);
                PushResponse pushResponse = client.getAcsResponse(iosRequest);

                //保存到数据库
                Notice notice = new Notice();
                notice.setGroupid(groupid);
                notice.setNoticebody(body);
                notice.setNoticetarget(target);
                notice.setNoticetitle(title);
                notice.setReceiver(receiver);
                notice.setSender(sender);
                notice.setNoticetype(noticetype);
                notice.setIsplay(0);
                noticeService.sendNotice(notice);
                result.setErrorCode(Rescode.SUCCESS, "发送成功");
            } catch (Exception e) {
                result.setErrorCode(Rescode.FAIL, "发送失败");
            }
            return result;
        } else {
            result.setErrorCode(Rescode.SUCCESS, "发送成功");
            return result;
        }
    }

    public static void main(String[] args) {
        PushRequest androidRequest = new PushRequest();
        //内容较大的请求，使用POST请求
        androidRequest.setMethod(MethodType.POST);
        androidRequest.setAppKey(Long.parseLong(Const.APPKEY));
        androidRequest.setTitle("测试标题");
        androidRequest.setBody("测试内容");
        androidRequest.setAcceptFormat(FormatType.JSON);
        androidRequest.setAndroidExtParameters("{\"type\":\""+1+"\",\"isplay\":\""+1+"\",\"tvalue\":\""+1+"\"}");
        androidRequest.setAndroidNotificationChannel("1");
        androidRequest.setPushType("NOTICE");
        androidRequest.setAndroidNotifyType("BOTH");
        androidRequest.setAndroidOpenType("ACTIVITY");
        androidRequest.setDeviceType("ANDROID");
        androidRequest.setTarget("ALL");
        androidRequest.setTargetValue("ALL");

        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "exjsVQTD9YD1zhou", "T6UafpL4wpqnzGndAg0b1aVWhDFeyf");
        DefaultAcsClient client = new DefaultAcsClient(profile);
        try {
            PushResponse pushNoticeToAndroidResponse = client.getAcsResponse(androidRequest);
            System.out.println(pushNoticeToAndroidResponse);
        } catch(Exception e) {
            e.printStackTrace();
        }

        /*try {
            PushNoticeToiOSRequest iosRequest = new PushNoticeToiOSRequest();
            //内容较大的请求，使用POST请求
            iosRequest.setMethod(MethodType.POST);
            iosRequest.setAppKey(Long.parseLong(Const.APPKEY));
            iosRequest.setTitle("测试推送标题");
            iosRequest.setBody("测试推送内容");
            iosRequest.setAcceptFormat(FormatType.JSON);
            iosRequest.setApnsEnv(Const.APNSENV);
            iosRequest.setTarget("ALL");
            iosRequest.setTargetValue("ALL");
            iosRequest.setExtParameters("{\"type\":\""+1+"\",\"isplay\":\""+1+"\",\"tvalue\":\"订单号\",\"content-available\":\"1\"}");
            IClientProfile profile1 = DefaultProfile.getProfile(Const.REGION, Const.ACCESSKEYID, Const.ACCESSKEYSECRET);
            DefaultAcsClient client1 = new DefaultAcsClient(profile1);
            PushNoticeToiOSResponse pushNoticeToiOSResponse = client1.getAcsResponse(iosRequest);
            System.out.println(pushNoticeToiOSResponse);
        }catch(Exception e) {
            e.printStackTrace();
        }*/
        /*try {
            IClientProfile profile1 = DefaultProfile.getProfile(Const.REGION, Const.ACCESSKEYID, Const.ACCESSKEYSECRET);
            DefaultAcsClient client1 = new DefaultAcsClient(profile1);
            PushRequest pushRequest = new PushRequest();
            // 推送目标
            pushRequest.setAppKey(Long.parseLong(Const.APPKEY));
            pushRequest.setTarget("ALL");
            pushRequest.setPushType("NOTICE"); // 消息类型 MESSAGE NOTICE
            pushRequest.setDeviceType("iOS");
            pushRequest.setTargetValue("ALL");
            pushRequest.setTitle("ALi Push Title"); // 消息的标题
            pushRequest.setBody("Ali Push Body");
            pushRequest.setIOSMusic("default"); // iOS通知声音
            pushRequest.setIOSSubtitle("iOS10 subtitle");//iOS10通知副标题的内容
            pushRequest.setIOSNotificationCategory("iOS10 Notification Category");
            pushRequest.setIOSMutableContent(true);
            pushRequest.setIOSApnsEnv("DEV");
            pushRequest.setIOSSilentNotification(true);
            PushResponse pushResponse = client1.getAcsResponse(pushRequest);
            System.out.printf("RequestId: %s, MessageID: %s\n",
                    pushResponse.getRequestId(), pushResponse.getMessageId());
        } catch (Exception e) {
            e.printStackTrace();
        }*/
    }
}
